/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.servlet.siter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 下载图片 证件照片
 * @author dusin
 */
@WebServlet(name = "GetLicenseJPG", urlPatterns = {"/servlet/siter/GetLicenseJPG"})
public class GetLicenseJPG extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //response.setHeader("content-type", "image/jpeg");
        response.setContentType("image/jpeg;charset=UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
        request.setCharacterEncoding("utf-8");
        
        FileInputStream fis=null;
        try {
           String filename=null ;  //如果没找到图片，则以此图片替代;
           File licenseFile=null;
           String plateNo = request.getParameter("plateNo");
           String dataDirectoryStr = this.getServletContext().getInitParameter("BussinessPhotoDir"); //存放业务数据照片目录
           String driverDirectoryStr = this.getServletContext().getInitParameter("DriverPhotoDir");//司机照片目录
           
           File dataFile = new File(dataDirectoryStr+plateNo);
           File driverFile = new File(driverDirectoryStr+"dr"+plateNo);
           File notFoundFile = new File(driverDirectoryStr+"notfound.jpg");
           if(dataFile.exists()){
               licenseFile = dataFile;
               filename = plateNo;
           }else if(driverFile.exists()){
               licenseFile = driverFile;
               filename = plateNo;
           }else{
               licenseFile = notFoundFile;
               filename = "notfound.jpg";
           }
           fis = new FileInputStream(licenseFile);
           filename = URLEncoder.encode(filename,"UTF-8");
           response.setHeader("content-disposition", "attachment;filename="+filename);
           //执行fileOutputStream的输出操作
            int len = 1;
            byte[] b = new byte[1024];
            while((len=fis.read(b))!=-1){
                outputStream.write(b, 0, len);
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            if(fis!=null)
                fis.close();
            if(outputStream!=null)
                outputStream.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
